import 'dart:io';
import 'package:flutter_volcengine/src/debug_volcengine.dart';
import 'package:flutter_volcengine_provider/flutter_volcengine_provider.dart';
import 'package:ve_alog/ve_alog.dart';
import 'package:ve_apm/ve_apm.dart';

class FlutterVolcengine implements VolcengineProvider {
  @override
  void enableTraceApp(bool toggle) {
    if (toggle) {
      VeAlogImpl.instance.enable();
    } else {
      VeAlogImpl.instance.disable();
    }
  }

  @override
  void runVolcengineTraceApp(
    RootWidgetBuilder builder, {
    bool monitorNetWork = false,
    bool enableLog = false,
  }) {
    runTraceApp((navigatorObserver) {
      if (enableLog) {
        VeAlogImpl.instance.enable();
        VeAlogImpl.instance.enableConsoleLog();
      }
      if (monitorNetWork) {
        final currentOverride = HttpOverrides.current;
        HttpOverrides.global = ApmHttpOverrides(
          customHttpOverrides: currentOverride,
        );
      }
      return builder(navigatorObserver);
    });
  }

  @override
  Future<void> reportEvent(
    String eventName, {
    Map<String, dynamic>? category,
    Map<String, dynamic>? metric,
    Map<String, dynamic>? extra,
  }) async {
    ApmReporter.instance.reportEvent(
      eventName,
      category: category,
      metric: metric,
      extra: extra,
    );
  }

  @override
  void selectTab(String name) {
    //TODO: ApmNavigatorObserver.instance.selectTab();
  }

  @override
  void info({required String tag, required String message}) {
    VeAlogImpl.instance.info(message: message, tag: tag);
  }

  @override
  void debug({required String tag, required String message}) {
    VeAlogImpl.instance.debug(message: message, tag: tag);
  }

  @override
  void warn({required String tag, required String message}) {
    VeAlogImpl.instance.warn(message: message, tag: tag);
  }

  @override
  void error({required String tag, required String message}) {
    VeAlogImpl.instance.error(message: message, tag: tag);
  }

  @override
  APMDebug get apmDebug => DebugVolcengine();
}
